package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行聚合函数
 */
public class JDBCDemo8 {
    public static void main(String[] args) {
        //需求:查看每个班各多少人
        /*
            SELECT COUNT(*) count,c.name
            FROM class c
            JOIN student s ON c.id = s.class_id
            GROUP BY c.name
         */
        try (Connection connection = DBUtil.getConnection();){
            Statement statement = connection.createStatement();
            String sql = "SELECT COUNT(*) count,c.name " +
                    "FROM class c " +
                    "JOIN student s ON c.id = s.class_id " +
                    "GROUP BY c.name";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                int count = rs.getInt("count");
                String name = rs.getString("c.name");
                System.out.println("人数:"+count+"，班级:"+name);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
